package com.my.cloud.service.impl;

import com.my.cloud.mapper.AccountMapper;
import com.my.cloud.service.AccountService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/14 11:33
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    
    @Resource
    private AccountMapper accountMapper;
    
    @Override
    public void decrease(Long userId, Long money) {
        log.info("------------> 账户开始扣减");
        accountMapper.decrease(userId, money);
        myTimeOut();
        //int i = 10 / 0;
        log.info("------------> 账户扣减完成");
    }

    /**
     * 模拟超时异常，全局事务回滚
     */
    private void myTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(65);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
